{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Sum With Exactly K Elements "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maximizeSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #K 个元素的最大和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code> 和一个整数&nbsp;<code>k</code>&nbsp;。你需要执行以下操作<strong>&nbsp;恰好</strong> <code>k</code>&nbsp;次，最大化你的得分：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>从 <code>nums</code>&nbsp;中选择一个元素&nbsp;<code>m</code>&nbsp;。</li>\n",
    "\t<li>将选中的元素&nbsp;<code>m</code>&nbsp;从数组中删除。</li>\n",
    "\t<li>将新元素&nbsp;<code>m + 1</code>&nbsp;添加到数组中。</li>\n",
    "\t<li>你的得分增加&nbsp;<code>m</code>&nbsp;。</li>\n",
    "</ol>\n",
    "\n",
    "<p>请你返回执行以上操作恰好 <code>k</code>&nbsp;次后的最大得分。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [1,2,3,4,5], k = 3\n",
    "<b>输出：</b>18\n",
    "<b>解释：</b>我们需要从 nums 中恰好选择 3 个元素并最大化得分。\n",
    "第一次选择 5 。和为 5 ，nums = [1,2,3,4,6] 。\n",
    "第二次选择 6 。和为 6 ，nums = [1,2,3,4,7] 。\n",
    "第三次选择 7 。和为 5 + 6 + 7 = 18 ，nums = [1,2,3,4,8] 。\n",
    "所以我们返回 18 。\n",
    "18 是可以得到的最大答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [5,5,5], k = 2\n",
    "<b>输出：</b>11\n",
    "<b>解释：</b>我们需要从 nums 中恰好选择 2 个元素并最大化得分。\n",
    "第一次选择 5 。和为 5 ，nums = [5,5,6] 。\n",
    "第二次选择 6 。和为 6 ，nums = [5,5,7] 。\n",
    "所以我们返回 11 。\n",
    "11 是可以得到的最大答案。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= k &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-sum-with-exactly-k-elements](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-sum-with-exactly-k-elements](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,5]\\n3', '[5,5,5]\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(k):\n",
    "            max_index = nums.index(max(nums))\n",
    "            res += nums[max_index]\n",
    "            nums[max_index] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        return (m+m+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        max = nums[0]\n",
    "        for i in nums:\n",
    "            if i > max:\n",
    "                max = i\n",
    "        return ((max + max + k - 1)*k // 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return k * max(nums) + k * (k - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        num = max(nums)\n",
    "        return int((num * 2 + k - 1) * k / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        '''res = 0\n",
    "        for _ in range(k):\n",
    "            i = max(nums)\n",
    "            res += i\n",
    "            nums[nums.index(i)] = i + 1\n",
    "        return res'''\n",
    "        return (max(nums) * 2 + k - 1) * k // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    maximizeSum = lambda self, nums, k: max(nums) * k + k * (k - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        an = max(nums)\n",
    "        return int((an + an + k -1)*k/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m=max(nums)\n",
    "        return (2*m+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (2 * max(nums) + k - 1) * k // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        target = max(nums)\n",
    "        return int((2 * target + k - 1) * k / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        mx = max(nums)\n",
    "        return (mx+mx+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return k * (2 * max(nums) + k - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + k * (k - 1) // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + (k - 1) * k // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        max_num = max(nums)\n",
    "        return max_num * k + sum(range(k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        ans = max(nums)\n",
    "        ans = (ans + ans + k - 1) * k // 2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(k):\n",
    "            res += max(nums)\n",
    "            nums[nums.index(max(nums))] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        maxV = max(nums)\n",
    "        # 等差数列求和\n",
    "        return (maxV + (maxV + k - 1)) * k // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (2 * max(nums) + k - 1) * k // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        return k * (m + m + k - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def maximizeSum(self, nums, k):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :type k: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return int((max(nums) + (k-1)/2) * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        max_ = max(nums)\n",
    "        return (2*max_ +k-1)*k // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        max_n = nums[0]\n",
    "        s = 0\n",
    "        for i in range(1,n):\n",
    "            max_n = max(max_n,nums[i])\n",
    "        for i in range(k):\n",
    "            s += max_n+i\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        a=max(nums)\n",
    "        return sum([i for i in range(a,a+k)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        num = max(nums)\n",
    "        return num*k + (k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + (k - 1) * k // 2\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        #选择最大的a,然后求a到a+k-1的和即可\n",
    "        #sum=kn+k(k-1)//2\n",
    "\n",
    "        return k*max(nums)+k*(k-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        maxx = max(nums)\n",
    "        source=0\n",
    "        for i in range(k):\n",
    "            source += maxx\n",
    "            maxx += 1\n",
    "        return source\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums)*k+k*(k-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        mx = max(nums)\n",
    "        mx_k = mx+(k-1)\n",
    "        return k * (mx+mx_k)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        a=0\n",
    "        for i in range(k):\n",
    "            m=max(nums)\n",
    "            nums.remove(m)\n",
    "            nums.append(m+1)\n",
    "            a += m\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        ans = 0\n",
    "        for i in range(k):\n",
    "            ans += nums[-1]\n",
    "            nums[-1] = nums[-1] + 1\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (max(nums)+max(nums)+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        extra = 0\n",
    "        for i in range(1, k):\n",
    "            extra += i\n",
    "        return max(nums) * k + extra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (max(nums) * k) + int((k-1)/2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums, k):\n",
    "        mm = 0\n",
    "        for i in nums:\n",
    "            if i >= mm:\n",
    "                mm = i\n",
    "        return int(mm*k+(k*(k-1)/2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        maxn=max(nums)\n",
    "        ans=0\n",
    "        return maxn*k+k*(k-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        ret = max(nums)*k\n",
    "        #for i in range(k):\n",
    "        #    ret += i\n",
    "        return ret + (k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        max_value = max(nums)\n",
    "        return (max_value + max_value + k - 1) * k // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        ans = 0\n",
    "        for i in range(k):\n",
    "            ans += m + i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        max_n = nums[0]\n",
    "        s = 0\n",
    "        for i in range(1,n):\n",
    "            max_n = max(max_n,nums[i])\n",
    "        for i in range(k):\n",
    "            s += max_n+i\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        return (m + m + k - 1) * k // 2\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return int(max(nums)*k+(k-1)*k/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return int((2 * max(nums) + k - 1) * k / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        tmp = max(nums)\n",
    "        tmp = tmp *k + (k-1)*k//2\n",
    "        return tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        n = max(nums)\n",
    "\n",
    "        return k*(2*n+k-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        a = 0\n",
    "        for i in range(k):\n",
    "            a +=i\n",
    "        \n",
    "        return max(nums)*k+1*a\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        maxnum=max(nums)\n",
    "        answer=int((2*maxnum+k-1)*k/2)\n",
    "        return answer\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (2 * max(nums) + k - 1) * k // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "       return k*max(nums)+k*(k-1)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + (k * (k-1) // 2)       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        n = max(nums)\n",
    "        return int(k*(2*n+k-1)/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        value = max(nums)\n",
    "        \n",
    "        result = 0\n",
    "        for i in range(k):\n",
    "            result += value\n",
    "            value += 1\n",
    "\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        return m*k + (k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 0:  return 0\n",
    "        m = max(nums)\n",
    "        return int(m * k + (k-1)*k/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        maxnum = max(nums)\n",
    "        return (maxnum + maxnum + k -1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (max(nums)*(k)+sum(range(k)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        m=max(nums)\n",
    "        return m * k + ((k-1) * k >> 1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        s = max(nums)\n",
    "        return int(s*k+(k*(k-1))/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        val = max(nums)\n",
    "        res = val * k + k * (k - 1) // 2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        ma = max(nums)\n",
    "\n",
    "        return (2*ma + k - 1) * k // 2 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        tmp = max(nums)\n",
    "        tmp = (tmp+tmp+k-1)*k//2\n",
    "        return tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return int(max(nums)*k + k*(k-1)/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return(k * (2 * max(nums) + k - 1)//2)\n",
    "       \n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        '''\n",
    "        res = 0\n",
    "        for i in range(k):\n",
    "            max_num = max(nums)\n",
    "            max_index = nums.index(max_num)\n",
    "            res += max_num\n",
    "            nums.pop(max_index)\n",
    "            nums.append(max_num + 1)\n",
    "        return res\n",
    "        '''\n",
    "        res = 0\n",
    "        max_num = max(nums)\n",
    "        res = k * (max_num + max_num + k - 1)// 2\n",
    "        return res\n",
    "\n",
    "        #x + x + x + x + 2 + ... + x + k - 1\n",
    "        #k()//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + sum(range(k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        if not nums:\n",
    "            return 0\n",
    "        m = max(nums)\n",
    "        return int((m + m + k - 1) * k / 2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        if not nums:\n",
    "            return 0\n",
    "        m = max(nums)\n",
    "        return int((m + m + k - 1) * k / 2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        tmp = max(nums)\n",
    "        return (tmp+tmp+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        res = []\n",
    "        for i in range(k):\n",
    "            a = max(nums)\n",
    "            index = nums.index(a)\n",
    "            res.append(a)\n",
    "            nums[index] = a+1\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        sum = max(nums)\n",
    "        sum = int(((k)*(sum+(sum+k-1)))/2)\n",
    "        return sum\n",
    "      \n",
    "      \n",
    "        '''for i in range(len(nums)):\n",
    "            if List[0] > List[i]:\n",
    "                sum = List[i]\n",
    "        sum += k\n",
    "        return sum'''\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) * k + k * (k - 1) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (2*max(nums)+k-1)*k//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        return (2 * max(nums) + k - 1) * k // 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximizeSum(self, nums: List[int], k: int) -> int:\n",
    "        res = 0\n",
    "        m = max(nums)\n",
    "        res = (2 * m + k - 1) * k / 2\n",
    "        return int(res)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
